package com.wutao.panzh.admin.content.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.panzh.UtilEntity.ResultVO;
import com.panzh.entity.FoodItem;
import com.panzh.entity.InformationItem;
import com.panzh.entity.RouteImag;
import com.panzh.entity.RouteItem;
import com.panzh.myconst.CommonConst;
import com.panzh.myconst.ProjectParames;
import com.wutao.panzh.service.RouteItemService;
import org.apache.tomcat.util.buf.UDecoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class RouteItemController {

    @Reference
    private RouteItemService routeItemService;

    //保存foodItem的方法,因为前台无法在对象中传入集合数据，所以用这种方式进行传值。
    @ResponseBody
    @RequestMapping("/save/routeItem")
    public ResultVO<String> saveInformationItem(@RequestParam("routeName") String routeName,
                                                @RequestParam("routeDay") Integer routeDay,
                                                @RequestParam("routeType") String routeType,
                                                @RequestParam("routeIndex") short routeIndex,
                                                @RequestParam("routeCostInfor") String routeCostInfor,
                                                @RequestParam("routeItemInforContent") String routeItemInforContent,
                                                @RequestParam("routeImags") String[] routeImags) {
        //这里应该是资讯名称不能相同，并不是ItemId，因为ItemId不可能重复；
//        String itemName = routeItem.getRouteName();
        List<RouteImag> imags = new ArrayList<>();
        if (routeImags == null) {
        } else {
            for (String routeImag : routeImags) {
                RouteImag imag = new RouteImag();
                imag.setRouteImagUrl(routeImag);
                imags.add(imag);
            }
        }
        RouteItem resultRouteItem = routeItemService.getRouteItemByItemName(routeName);
        //如果为null，说明没有该信息，但是如果查到了，就证明系统已经有重名的资讯，直接拒绝插入；
        if (resultRouteItem != null) {
            return new ResultVO<String>(ResultVO.FAILED, CommonConst.INFORMATION_ALREADY_EXISTS, ResultVO.NO_DATA);
        }
        //进行信息的插入；
        RouteItem routeItem = new RouteItem(routeName, routeDay, routeType, routeIndex, routeCostInfor, routeItemInforContent, imags);
        routeItemService.saveRouteItem(routeItem);
        return new ResultVO<String>(ResultVO.SUCCESS, CommonConst.INFORMATION_SAVED_SUCCESSFULLY, ResultVO.NO_DATA);
    }


    //    get/route/item/page
    //执行分页的方法：
    @ResponseBody
    @RequestMapping("/get/route/item/page")
    public ResultVO<PageInfo<RouteItem>> getUserPage(
            //由于pageNo在请求参数中不一定能够提供，所以设置默认值。
            //默认值为1表示未提供此数据时前往第1页
            @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
            //由于keyword在请求参数中不一定能够提供，所以设置默认值。
            //默认值为空字符串将不会对SQL语句有任何影响
            @RequestParam(value = "keyword", defaultValue = "") String keyword) {

        PageInfo<RouteItem> routeItemPageInfo = routeItemService.getRouteItemPageInfo(pageNo, ProjectParames.PAGE_SIZE, keyword);

        return new ResultVO<PageInfo<RouteItem>>(ResultVO.SUCCESS, ResultVO.NO_MSG, routeItemPageInfo);
    }

    //    get/routeItem/by/id
    @RequestMapping("/get/routeItem/by/id")
    @ResponseBody
    public ResultVO getInformatinById(@RequestParam("routeItemId") String routeItemId) {
        RouteItem routeItem = routeItemService.getRouteItemById(routeItemId);
        //未在数据库中查询到数据；
        if (routeItem == null) {
            return new ResultVO<String>(ResultVO.FAILED, CommonConst.INFORMATION_DOES_NOT_EXIST, ResultVO.NO_DATA);
        } else {
            //在数据库中查询到数据
            return new ResultVO<RouteItem>(ResultVO.SUCCESS, ResultVO.NO_MSG, routeItem);
        }
    }

    //删除InformationItem的方法；
    @RequestMapping("/delete/routeItem")
    @ResponseBody
    public ResultVO deleteInformationById(@RequestParam("routeItemId") String routeItemId) {
        //如果抛出异常，进行捕获后返回
        try {
            routeItemService.deleteRouteItem(routeItemId);
        } catch (Exception e) {
            return new ResultVO<String>(ResultVO.FAILED, CommonConst.DELETE_INFORMATION_EXCEPTION, ResultVO.NO_DATA);
        }
        return new ResultVO<String>(ResultVO.SUCCESS, CommonConst.DELETE_INFORMATION_SUCCESS, ResultVO.NO_DATA);
    }

    //    href='update_route?itemId="+id+"'
    @RequestMapping("update_route")
    public String getUpdateRoute(@RequestParam("itemId") String itemId, Model model) {
        RouteItem routeItem = routeItemService.getRouteItemById(itemId);
        model.addAttribute("routeItem", routeItem);
        return "update_route";
    }


    //进行routeItem的更新
    @ResponseBody
    @RequestMapping("/update/routeItem")
    public ResultVO<String> updateRouteItem(@RequestParam("id") Integer id,
                                            @RequestParam("routeName") String routeName,
                                            @RequestParam("routeDay") Integer routeDay,
                                            @RequestParam("routeType") String routeType,
                                            @RequestParam("routeIndex") short routeIndex,
                                            @RequestParam("routeCostInfor") String routeCostInfor,
                                            @RequestParam(value = "routeItemInforContent" ,required = false,defaultValue = "") String routeItemInforContent,
                                            @RequestParam(value = "routeImags", required = false) String[] routeImags) {
        List<RouteImag> imags = new ArrayList<>();

        if (routeImags == null) {
        } else {
            for (String routeImag : routeImags) {
                RouteImag imag = new RouteImag();
                imag.setRouteImagUrl(routeImag);
                imags.add(imag);
            }
        }
        //进行数据的插入，就这样写吧，懒得多写一个构造函数了。
        RouteItem routeItem = new RouteItem(routeName, routeDay, routeType, routeIndex, routeCostInfor, routeItemInforContent, imags);
        routeItem.setId(id);
        //这里应该是资讯名称不能相同，并不是ItemId，因为ItemId不可能重复，检查有没有同名的；
        //调用方法获得同名的RouteItem,注意，本身可能存在数据库，可能会把本身查出来，所以必须要验证List的长度；
        List<RouteItem> routeItems = routeItemService.getRouteAllByItemName(routeName);
        //这里涉及一点小算法,只可能等于0或者等于1,如若size等于0，直接进行更新， 若size等于1，则比较id，若是id相同，则可以进行更新，若不同,拒绝更新
        if (routeItems.size() == 0) {
            routeItemService.updateRouteItem(routeItem);
            return new ResultVO<String>(ResultVO.SUCCESS, CommonConst.INFORMATION_UPDATED_SUCCESSFULLY, ResultVO.NO_DATA);
        } else {
            RouteItem resultRouteItem = routeItems.get(0);
            if (resultRouteItem.getId().compareTo(id) == 0) {
                routeItemService.updateRouteItem(routeItem);
                return new ResultVO<String>(ResultVO.SUCCESS, CommonConst.INFORMATION_UPDATED_SUCCESSFULLY, ResultVO.NO_DATA);
            }
        }
        return new ResultVO<String>(ResultVO.FAILED, CommonConst.INFORMATION_ALREADY_EXISTS, ResultVO.NO_DATA);
    }
}
